package main

import (
	"fmt"
	"github.com/streadway/amqp"
	"log"
	"time"
)

func main() {
	dc, err := mqConnect()
	if err != nil {
		panic(err)
	}

	if err := dc.ExchangeDeclare("demo1", "topic", false, false, false, false, nil); err != nil {
		panic(err)
	}

	if err := dc.Publish("demo1", "d", false, false, amqp.Publishing{
		ContentType: "text/plain",
		Priority:    5,
		Timestamp:   time.Now(),
		UserId:      "guest",
		AppId:       "guest",
	}); err != nil {
		panic(err)
	}
}

func mqConnect() (*amqp.Channel, error) {
	rabbitUrl := fmt.Sprintf("amqp://%s:%s@%s:%d/", "guest", "guest", "demo", 5673)
	dial, err := amqp.Dial(rabbitUrl)
	if err != nil {
		log.Printf("amqp.Dial err: %v\n", err)
		return nil, err
	}

	dc, err := dial.Channel()
	if err != nil {
		log.Printf("dial.Channel err: %v\n", err)
		return nil, err
	}

	return dc, nil
}
